package com.example.demo.controller;

import com.example.demo.pojo.Book;
import com.example.demo.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController //RESTful 是一种标准。
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "hello";
    }
    //GetMapping /PostMapping/PutMapping/DeleteMapping
    @GetMapping("/test")
    public Map<String,String> testMap(){
        HashMap<String,String> map = new HashMap<>();
        map.put("name","张三");
        map.put("age","18");
        return map; //JSON XML

    }
    @GetMapping("/book")
    public Book testBook(){
        Book book = new Book();
        book.setBookName("java");
        book.setPublisher("清华大学出版社");
        return book;

    }
    @GetMapping("/arr")
    public String[] testArr(){
        String[] arr = {"java","python","c++"};
        return arr;
    }

    @GetMapping("/bookList")
    public Book[] testBookList(){
        Book[] arr = new Book[2];
        Book book = new Book();
        book.setBookName("java");
        book.setPublisher("清华大学出版社");
        arr[0] = book;
        Book book1 = new Book();
        book1.setBookName("python");
        book1.setPublisher("北京大学出版社");
        arr[1] = book1;
        return arr;

    }
    @GetMapping("/bookList2")
    public List<Book> testBookList2(){
        List<Book> list = new ArrayList<>();
        Book book = new Book();
        book.setBookName("java");
        book.setPublisher("清华大学出版社");
        list.add(book);
        Book book1 = new Book();
        book1.setBookName("python");
        book1.setPublisher("北京大学出版社");
        list.add(book1);
        return list;
    }

    @Autowired
    private Person person; //Spring提供的功能，对象组装。 基于类型的组装 。 依赖注入

    @GetMapping("/person")
    public Person testPerson(){
        return person;
    }
}
